package handlers

import (
	"219yk/api/requset"
	"219yk/api/response"
	"219yk/dao"
	"219yk/models"
	"github.com/gin-gonic/gin"
)

func AddRoom(c *gin.Context) {
	var data requset.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if !dao.CreateRoom(int(c.GetUint("userId"))) {
		response.ResponseError(c, "创建失败")
		return
	}
	response.ResponseSuccess(c, "创建成功")
}

func AddUserRoom(c *gin.Context) {
	var data requset.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if !dao.RoomGet(data.RoomId) {
		response.ResponseError(c, "群不存在")
		return
	}
	if !dao.UserGet(data.RoomUserId) {
		response.ResponseError(c, "用户不存在")
		return
	}
	userroom := dao.GetULeveL(int(c.GetUint("userId")))
	if userroom.ULevel != 1 && userroom.ULevel != 2 {
		response.ResponseError(c, "你不配")
		return
	}
	user := models.RoomUser{
		RoomUserId: data.RoomUserIds,
		UserId:     int(c.GetUint("userId")),
		RoomId:     data.RoomId,
		ULevel:     3,
	}
	if !dao.CreateRoomUser(user) {
		response.ResponseError(c, "添加失败")
		return
	}
	response.ResponseSuccess(c, "添加成功")

}

func UpdateUlevel(c *gin.Context) {
	var data requset.UpdateUlevel
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if !dao.RoomGet(data.RoomId) {
		response.ResponseError(c, "群不存在")
		return
	}
	userroom := dao.GetULeveL(int(c.GetUint("userId")))
	if userroom.ULevel != 1 {
		response.ResponseError(c, "你不配")
		return
	}
	user := models.RoomUser{
		RoomUserId: userroom.RoomUserId,
		UserId:     int(c.GetUint("userId")),
		RoomId:     userroom.RoomId,
		ULevel:     data.ULevel,
	}
	if !dao.UpdateUlevel(user) {
		response.ResponseError(c, "修改失败")
		return
	}
	response.ResponseSuccess(c, "修改成功")

}

func DelRoom(c *gin.Context) {
	var data requset.DelRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	userroom := dao.GetULeveL(int(c.GetUint("userId")))
	if userroom.ULevel != 1 {
		response.ResponseError(c, "你不配")
		return
	}
	if !dao.DelRoom(data.RoomId) {
		response.ResponseError(c, "删除失败")
		return
	}
	response.ResponseSuccess(c, "删除成功")
}
